#include<cstdio>
#include<cstring>
const int N=110;
const int INF=1<<30;
int f[N][N];
int n,m;
int s1[N],s2[N];
int g[5][5]={{5,-1,-2,-1,-3},
			 {-1,5,-3,-2,-4},
			 {-2,-3,5,-2,-2},
			 {-1,-2,-2,5,-1},
			 {-3,-4,-2,-1,0}};
int max(int x,int y){return x>y?x:y;}
int main()
{
	int t;
	char ch;
	scanf("%d",&t);
	while(t--)
	{
		memset(f,0,sizeof(f));
		scanf("%d%c",&n,&ch);
		for(int i=1;i<=n;i++)
		{
			scanf("%c",&ch);
			if(ch=='A')s1[i]=0;
			if(ch=='C')s1[i]=1;
			if(ch=='G')s1[i]=2;
			if(ch=='T')s1[i]=3;
			f[i][0]=f[i-1][0]+g[4][s1[i]];
		}
		scanf("%d%c",&m,&ch);
		for(int i=1;i<=m;i++)
		{
			scanf("%c",&ch);
			if(ch=='A')s2[i]=0;
			if(ch=='C')s2[i]=1;
			if(ch=='G')s2[i]=2;
			if(ch=='T')s2[i]=3;
			f[0][i]=f[0][i-1]+g[4][s2[i]];
		}
		for(int i=1;i<=n;i++)
			for(int j=1;j<=m;j++)
			{
				f[i][j]=-INF;
				f[i][j]=max(f[i-1][j]+g[4][s1[i]],f[i][j]);
				f[i][j]=max(f[i][j-1]+g[4][s2[j]],f[i][j]);
				f[i][j]=max(f[i-1][j-1]+g[s1[i]][s2[j]],f[i][j]);
			}
		printf("%d\n",f[n][m]);
	}
	return 0;
}



				


